let publicKey = '';
let privateKey = '';
let encryptedInput = '';
let decryptedInput = '';

// Generate private key
function createPrivateKey() {
    privateKey = '321CBA'; // Example key
}

// Generate public key
function createPublicKey() {
    publicKey = 'ABC123'; // Example key
}

// Function to dynamically substitute characters based on input
function dynamicSubstitute(input, chars, replacements) {
    let substitutionMap = {};
    
    // Create a map from chars to replacements
    for (let i = 0; i < chars.length; i++) {
        substitutionMap[chars[i]] = replacements[i];
    }

    // Apply the substitution map to the input string
    return input.split('').map(char => substitutionMap[char] || char).join('');
}

// Encrypt input using dynamic substitution and reverse the result
function encryptInput(input) {
    createPublicKey();  // Generate public key
    createPrivateKey(); // Generate private key

    const originalChars = 'Hello, World!';
    const replacementChars = 'ABCC123W1rCd!';

    // Perform substitution and then reverse the string
    encryptedInput = dynamicSubstitute(input, originalChars, replacementChars).split('').reverse().join('');

    console.log('Made Key...', privateKey); // Log private key
    console.log('Encrypted...', encryptedInput); // Log encrypted output
    console.log('Made Key...', publicKey); // Log public key
}

// Decrypt input by reversing and then applying the reverse substitution
function decryptInput(key) {
    if (key === privateKey) {
        const originalChars = 'Hello, World!';
        const replacementChars = 'ABCC123W1CCd!';

        let reverseSubstitutionMap = {};

        // Create a reverse map from replacementChars to originalChars
        for (let i = 0; i < replacementChars.length; i++) {
            reverseSubstitutionMap[replacementChars[i]] = originalChars[i];
        }

        // Reverse the string, then apply the reverse substitution map
        decryptedInput = encryptedInput.split('').reverse().map(char => reverseSubstitutionMap[char] || char).join('');

        console.log('Using key...', key); // Log private key
        console.log('Using key...', publicKey); // Log public key
        console.log('Decrypted...', decryptedInput); // Log decrypted output
    } else {
        console.log('Decryption failed: Key mismatch');
    }
}

const stringInput = "Hello, World!";

console.log('Receiving...');

encryptInput(stringInput);

console.log('Decrypting...');

decryptInput(privateKey);

console.log('Finishing...');
